package cn.fsw.pethome.system.service.impl;

import cn.fsw.pethome.base.service.impl.BaseServiceImpl;
import cn.fsw.pethome.system.domain.Menu;
import cn.fsw.pethome.system.mapper.MenuMapper;
import cn.fsw.pethome.system.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fsw
 * @since 2022-10-19
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> menuTree() {
        List<Menu> menus = menuMapper.selectAll();
        List<Menu> first = new ArrayList<>();
        for (Menu menu : menus) {
            if(menu.getParent_id() == null){
                first.add(menu);
            }else {
                for (Menu menu2 : menus) {
                    if(menu.getParent_id() == menu2.getId()){
                        menu2.getChildren().add(menu);
                    }
                }
            }
        }
        first.forEach(System.out::println);
        return first;
    }
}
